import { _decorator, animation, Component, find, instantiate, Node, Prefab, randomRangeInt, tween, UITransform, Vec3 } from 'cc';
import { sun } from './sun';
import { PlantState } from './enums';
const { ccclass, property } = _decorator;


@ccclass('plant_base')
export class plant_base extends Component {
    @property(Prefab)
    private m_sunPrefab: Prefab = null;
    @property
    private growthRate: number = 5;
    private currentGrowthTime : number = 0;
    private animCtrl : animation.AnimationController = null;
    private plantType: PlantState = PlantState.DISABLE;
    protected onLoad(): void {
        this.animCtrl = this.getComponent(animation.AnimationController);
        if(!this.animCtrl)
        {
            console.log("ZBL AnimationController Componet not find");
        }
        this.plantType = PlantState.DISABLE;
    }

    update(deltaTime: number) {
        if(this.plantType === PlantState.DISABLE)
        {
            return;
        }
        this.currentGrowthTime -= deltaTime;
        if(this.currentGrowthTime <= 0)
        {
            //生产出阳光并播放生长动画
            this.animCtrl.setValue("grow", true);
            this.currentGrowthTime = this.growthRate;
            console.log("Plant is ready to grow");
        }
    }

    public startGrow()
    {
       this.plantType = PlantState.ENABLE;
       this.currentGrowthTime = this.growthRate;
    }

    public burnSun(): void
    {
        console.log("burnSun==========");
        this.animCtrl.setValue("grow", false);
        let obj = instantiate(this.m_sunPrefab);
        let objParentNode = find('Canvas/sunList');
        if(!objParentNode)
        {
            console.log("sunList node not find");
            return;
        }
        obj.setParent(objParentNode);
        //obj.setWorldPosition(this.node.worldPosition);
        //将世界坐标转换为节点局部坐标
        let transform = objParentNode.getComponent(UITransform);
        if(transform)
        {
           let localPos = transform.convertToNodeSpaceAR(this.node.worldPosition);
           obj.setPosition(localPos);
        }
        // 为了更清晰地看到移动过程，可以适当延长动画时间
    tween(obj)
        // 第一个动画：向上移动50单位，用时2秒（较慢，便于观察）
        .to(1, { 
            position: new Vec3(obj.position.x, obj.position.y + 50, obj.position.z) 
        }, {
            // 增加进度回调，观察动画过程
            onUpdate: (target: any, ratio: number) => {
                //console.log(`第一阶段移动进度: ${(ratio * 100).toFixed(1)}%`);
            }
        })
        // 第二个动画：左右随机移动，用时1秒（适中速度）
        .to(1,  // 适当延长时间，更容易观察
            { 
                position: new Vec3(
                    obj.position.x + randomRangeInt(-100, 100), 
                    obj.position.y, 
                    obj.position.z
                ) 
            },
            { 
                easing: 'sineInOut',
                onUpdate: (target: any, ratio: number) => {
                    //console.log(`第二阶段移动进度: ${(ratio * 100).toFixed(1)}%`);
                }
            }
        )
        .call(() => {
            console.log("----tween complet----");
        })
        .start();
    }
}


